//
//  HomeStatusModel.swift
//  ifanr
//
//  Created by 😱😱 on 16/9/14.
//  Copyright © 2016年 😱😱. All rights reserved.
//

import Foundation

/**
 *  作者信息
 */
struct AuthorInfoModel {
    var job: String!
    var name: String!
    var avatar: String!
    var description: String!
    
    init(dict: NSDictionary) {
        self.job = dict["job"] as? String ?? ""
        self.name = dict["name"] as? String ?? ""
        self.avatar = dict["avatar"] as? String ?? ""
        self.description = dict["description"] as? String ?? ""
    }
}

/**
 发布类型： 在计算cell高度是需要分类进行计算（目前只发现这几种）
 */
enum PostType {
    case post
    case data
    case dasheng
}


struct HomeStatusModel: Initable {
    /// id
    var ID: String!
    /// 标题
    var title: String!
    /// 作者
    var author: String!
    /// 时间
    var pubDate: String!
    /// 发布时间
    var post_modified: String!
    /// 图片
    var image: String!
    /// cwb_image_url
    var cwb_image_url: String!
    /// 内容
    var content: String!
    /// 摘录
    var excerpt: String!
    /// 引文
    var introduce: String!
    /// 网页版
    var link: String!
    /// 评论数
    var comments: String!
    /// 分类
    var category: String!
    /// 分类网页
    var category_link: String!
    /// tag
    var tags: String!
    /// 喜欢数
    var like: Int!
    var is_ad: Int!
    /// 作者信息
    var authorInfoModel: AuthorInfoModel!
    /// 发布类型
    var post_type: PostType! = .post
    
    //MARK: --------------------------- Home --------------------------
    /// 大声作者
    var dasheng_author: String! = ""
    var dasheng_original: String! = ""
    var dasheng_comment: String! = ""
    var dasheng_raw_content: String! = ""
    
    
    /// 数读
    var number: String! = ""
    var subfix: String! = ""
    
    init(dict: NSDictionary) {
        initCommonData(dict)
        
        initHomeData(dict)
    }
}

extension HomeStatusModel {
    mutating func initCommonData(dict: NSDictionary) {
        let idStr: NSInteger = dict["ID"] as? NSInteger ?? 0
        self.ID = "\(idStr)"
        self.title = dict["title"] as? String ?? ""
        self.author = dict["author"] as? String ?? ""
        self.pubDate = dict["pubDate"] as? String ?? ""
        self.post_modified = dict["post_modified"] as? String ?? ""
        self.image = dict["image"] as? String ?? ""
        self.cwb_image_url = dict["cwb_image_url"] as? String ?? ""
        self.content = dict["content"] as? String ?? ""
        self.excerpt = dict["excerpt"] as? String ?? ""
        self.introduce = dict["introduce"] as? String ?? ""
        self.link = dict["link"] as? String ?? ""
        self.comments = dict["comments"] as? String ?? ""
        self.category = dict["category"] as? String ?? ""
        self.category_link = dict["category_link"] as? String ?? ""
        self.tags = dict["tags"] as? String ?? ""
        self.like = dict["like"] as? Int ?? 0
        self.is_ad = dict["is_ad"] as? Int ?? 0
        
        if let item: NSDictionary = (dict["author_info"] as? NSDictionary) {
            self.authorInfoModel = AuthorInfoModel(dict: item)
        }
    }
    
    mutating func initHomeData(dict: NSDictionary) {
        if let type = dict["post_type"] as? String {
            if type == "post" {
                self.post_type = .post
            } else if type == "dasheng" {
                self.post_type = .dasheng
                self.dasheng_author = dict["dasheng_author"] as? String ?? ""
                self.dasheng_comment = dict["dasheng_comment"] as? String ?? ""
                self.dasheng_original = dict["dasheng_original"] as? String ?? ""
                self.dasheng_raw_content = dict["dasheng_raw_content"] as? String ?? ""
                self.category = "大声"
            } else if type == "data" {
                self.post_type = .data
                self.category = "数读"
                self.number = dict["number"] as? String ?? ""
                self.subfix = dict["subfix"] as? String ?? ""
            }
        }
    }
}

//{
//    "ID": 716904,
//    "title": "未来的人工智能什么样？是让我们离不开，还是灭掉我们？",
//    "author": "爱范儿",
//    "pubDate": "2016-09-13 21:33:36",
//    "post_timestamp": 1473773616,
//    "post_modified": "2016-09-13 17:53:36",
//    "image": "http://images.ifanr.cn/wp-content/uploads/2016/09/sat-ai-head.jpg",
//    "cwb_image_url": "",
//    "content": "<div class=\"dasheng_content clearfix\">\n<p> </p>\n</div>",
//    "excerpt": "科大讯飞创始人胡郁：在过去的几年当中，大家已经接受互联网和移动互联网作为基础设施，像曾经的水和电一样进入到我们的生活。现在大家能够看到，在将来的 10 年，人工智能也会像技术服务进入到我们的生活当中，每个人都离不开。",
//    "introduce": "",
//    "link": "http://www.ifanr.com/dasheng/716904",
//    "comments": "3",
//    "category": "大声",
//    "post_type": "dasheng",
//    "category_link": "",
//    "tags": "C＋峰会|人工智能|出门问问|新浪|李志飞|科大讯飞|红杉资本",
//    "like": 0,
//    "is_ad": 0,
//    "dasheng_author": "科大讯飞创始人胡郁",
//    "dasheng_original": "http://tech.sina.com.cn/it/2016-09-13/doc-ifxvukhv8284608.shtml",
//    "dasheng_comment": "<p></p>\n",
//    "dasheng_raw_content": "在过去的几年当中，大家已经接受互联网和移动互联网作为基础设施，像曾经的水和电一样进入到我们的生活。现在大家能够看到，在将来的 10 年，人工智能也会像技术服务进入到我们的生活当中，每个人都离不开。",
//    "author_info": {
//        "job": "",
//        "name": "ifanr",
//        "avatar": "http://images.ifanr.cn/wp-content/uploads/2016/05/爱范儿客户端-logo.png",
//        "description": "我们报道未来，服务新生活引领者。"
//    }
//},
//{
//    "ID": 716548,
//    "title": "昨天无人机诞生 100 年了，他们却打算改变整个行业",
//    "author": "李 赓",
//    "pubDate": "2016-09-13 20:20:34",
//    "post_timestamp": 1473769234,
//    "post_modified": "2016-09-14 10:49:43",
//    "image": "http://ifanr-cdn.b0.upaiyun.com/wp-content/uploads/2016/09/drone3.gif",
//    "cwb_image_url": "",
//    "content": "<p></p>\n",
//    "excerpt": "在其他结构布局无人机的特性优势有所体现之后，无人机市场中的技术和资本力量必将“此消彼长”，整个市场还可能会迎来一个更大的“浪潮”。而领跑者只会出现在“先知先觉”的人当中。",
//    "introduce": "在其他结构布局无人机的特性优势有所体现之后，无人机市场中的技术和资本力量必将“此消彼长”，整个市场还可能会迎来一个更大的“浪潮”。而领跑者只会出现在“先知先觉”的人当中。",
//    "link": "http://www.ifanr.com/716548",
//    "comments": "13",
//    "category": "商业",
//    "post_type": "post",
//    "category_link": "http://www.ifanr.com/category/special/business",
//    "tags": "AOSSCI|傲势|固定翼|无人机",
//    "like": 0,
//    "is_ad": 0,
//    "author_info": {
//        "job": "高级编辑",
//        "name": "ligeng",
//        "avatar": "http://images.ifanr.cn/wp-content/uploads/2015/10/IMG_1575111.jpg",
//        "description": "鲜衣怒马少年。"
//}
